package com.example.demo.order.service;

import com.example.demo.address.bean.AddressBean;
import com.example.demo.order.bean.OrderBean;
import com.example.demo.order.bean.OrderItemBean;
import com.example.demo.order.repository.OrderRepository;
import com.example.demo.service.BaseService;
import com.example.demo.shops.bean.ShopBean;
import com.example.demo.shops.service.ShopService;
import com.example.demo.user.bean.UserBean;
import com.example.demo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderService extends BaseService<OrderBean,OrderRepository>{

    @Autowired
    ShopService shopService;

    @Autowired
    OrderItemService orderItemService;


    public List<OrderItemBean> getShopCar(String[] items, HashMap<Integer, Integer> hashMap){
        List<OrderItemBean> list = new ArrayList<>();
        for (String item : items) {
            if (hashMap.containsKey(item)) {
                ShopBean shopBean = shopService.getById(item);
                OrderItemBean orderItemBean = new OrderItemBean(shopBean,hashMap.get(item));
                list.add(orderItemBean);
            }
        }
        return list;
    }

    public void addOrder(List<OrderItemBean> itemBeanList, UserBean userBean, AddressBean addressBean){

        OrderBean orderBean = new OrderBean(itemBeanList,userBean,addressBean);
        save(orderBean);

        for (OrderItemBean orderItemBean : itemBeanList) {
            orderItemBean.setOrderBean(orderBean);
        }
        orderItemService.repository.save(itemBeanList);
    }
}
